<?php

namespace app\admin\model\wdsxh;

use think\Model;


class Member extends Model
{

    

    

    // 表名
    protected $name = 'wdsxh_member';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'index_text',
        'gender_text',
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getIndexList()
    {
        return ['0' => __('Index 0'), '1' => __('Index 1')];
    }


    public function getGenderList()
    {
        return ['0' => __('Gender 0'), '1' => __('Gender 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIndexTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['index']) ? $data['index'] : '');
        $list = $this->getIndexList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGenderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
        $list = $this->getGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function level(){
        return $this->belongsTo('app\admin\model\wdsxh\MemberUnitLevel','level_id','id')->field('id,name,expense,deadline,introduce');
    }

    public function user(){
        return $this->belongsTo('app\admin\model\User','uid','id');
    }
    public function company(){
        return $this->hasOne('app\admin\model\wdsxh\MemberUnit','member_id','id');
    }

    public function commerce(){
        return $this->belongsTo('app\admin\model\PrefecturalChamberOfCommerce','commerce_id','id');
    }

}
